using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ScanConfirm.Entity;
using ScanConfirm.Model;
using Leeder.Service;
using Leeder.Repository;

namespace ScanConfirm.Service
{
    /// <summary>
    /// 
    /// </summary>
   public class QueueInfoService : BaseService<QueueInfoModel>
   {
        /// <summary>
        /// 关联实体类型
        /// </summary>
        protected override Type RefEntityType { get { return typeof(QueueInfoEntity); } }

        /// <summary>
        /// 获取队列
        /// </summary>
        /// <param name="queueLength">队列长度</param>
        /// <returns></returns>
        public List<QueueInfoModel> GetWorkQueue(string where,int queueLength)
        {
            try
            {
                return RF<QueueInfoEntity>.SqlQueryable("SELECT  * FROM SC_QueueInfo " + where + " ORDER BY CreatedDate ASC OFFSET 0 ROWS FETCH NEXT " + queueLength + " ROWS ONLY ").Select(s => new QueueInfoModel
                {
                    ID = s.ID,
                    Sequence = s.Sequence,
                    BodyNum = s.BodyNum,
                    EnqueueDate = s.EnqueueDate,
                    DeQueueDate = s.DeQueueDate,
                    CreatedDate = s.CreatedDate,
                    VIN = s.VIN,
                    CarModelCode = s.CarModelCode,
                    VehicleModel = s.VehicleModel,
                    PartsCode = s.PartsCode,
                    Mark = s.Mark,
                    DisplayCode = s.DisplayCode,
                    IsNEV = s.IsNEV,
                    IsComplete = s.IsComplete,
                    CompleteDate = s.CompleteDate,
                    Status = s.Status,
                    SyncFlag = s.SyncFlag,
                }).ToList();
            }
            catch(Exception ex)
            {
                LogObj.Error(ex.ToString());
                return default;
            }
        }

        /// <summary>
        /// 根据条件获取数据总量
        /// </summary>
        /// <param name="where">查询条件</param>
        /// <returns></returns>
        public int GetQueueCountByWhere(string where)
        {
            return RF<QueueInfoEntity>.SqlQueryable("SELECT  * FROM SC_QueueInfo " + where).Count();
        }

        /// <summary>
        /// 根据条件获取指定分页指定数量的记录
        /// </summary>
        /// <param name="where">查询条件</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">单页数据数量</param>
        /// <returns></returns>
        public List<QueueInfoModel> GetQueueByPage(string where, int pageIndex, int pageSize)
        {
            if (pageIndex < 1)
                return null;
            pageIndex = (pageIndex - 1) * pageSize;
            return RF<QueueInfoEntity>.SqlQueryable("SELECT  * FROM SC_QueueInfo " + where + " ORDER BY CreatedDate DESC OFFSET " + pageIndex + " ROWS FETCH NEXT " + pageSize + " ROWS ONLY ").Select(s => new QueueInfoModel
            {
                ID = s.ID,
                Sequence = s.Sequence,
                BodyNum = s.BodyNum,
                EnqueueDate = s.EnqueueDate,
                DeQueueDate = s.DeQueueDate,
                CreatedDate = s.CreatedDate,
                VIN = s.VIN,
                CarModelCode = s.CarModelCode,
                VehicleModel = s.VehicleModel,
                PartsCode = s.PartsCode,
                Mark = s.Mark,
                DisplayCode = s.DisplayCode,
                IsNEV = s.IsNEV,
                IsComplete = s.IsComplete,
                CompleteDate = s.CompleteDate,
                Status = s.Status,
                SyncFlag = s.SyncFlag,
            }).ToList();
        }
    }
}